#!/opt/ActivePerl-5.10/bin/perl
#/usr/bin/perl
use strict;
use warnings;

use File::Xcopy; 
use Getopt::Long;
use Config::Simple;
use File::Util;
use Carp  qw/confess/;

my $ConfigFile   = undef;
my $Profile=undef;

GetOptions (
	"c|config=s" => \$ConfigFile,  
	"p|Profile=s" => \$Profile   
	); 

$ConfigFile="backup.ini" unless defined $ConfigFile;
die ( "Missing config file : $ConfigFile" ) unless  -f "$ConfigFile";

$Profile="Default" unless defined $Profile;

tie my %cfg, "Config::Simple", $ConfigFile;

my $Source=$cfg{"$Profile.Source"};
confess ("Profile missing : $Profile" ) unless defined $Source and -d "$Source";

my $Destination=$cfg{"$Profile.Destination"};
mkpath $Destination, 1 unless (-d $Destination);

my $verbose=$cfg{"$Profile.Verbose"};

my $logFile=$cfg{"$Profile.LogFile"};
touch($logFile) unless -f $logFile;
my $separator="\n" . "#"x80 . "\n";
File::Util->new()->write_file('file' => $logFile, 'content' => $separator, 'mode' => 'append' );
my $IncludePattern=$cfg{"$Profile.IncludePattern"};

my $Action=$cfg{"$Profile.Action"};

my $fx = new File::Xcopy; 
$fx->from_dir($Source);
$fx->to_dir($Destination);
$fx->fn_pat($IncludePattern);  

$fx->param('s',1);  
$fx->param('verbose',$verbose);     
$fx->param('log_file', $logFile);

my ($sr, $rr) = $fx->get_stat; 
$fx->execute($Action);

exit 0;
